package com.javaee6.votinator;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class UserService {

	@PersistenceContext
	private EntityManager em;

	public void create(User entity) {
		em.persist(entity);
	}

	public void update(User entity) {
		em.merge(entity);
	}

	public void remove(User entity) {
		em.remove(em.merge(entity));
	}

	public User find(Object id) {
		return em.find(User.class, id);
	}

	public void createnewData(Long id) {
		User user = find(id);
		String fileName = user.hashCode() + ".xhtml";
		
	}

	public Long login(String email, String pw) throws NoSuchUserException {
		List<User> resultlist = em
				.createQuery(
						"select u from User u where u.email =:email and u.pw=:pw")
				.setParameter("email", email).setParameter("pw", pw)
				.getResultList();
		System.out.println(resultlist);
		if (resultlist != null && resultlist.size() > 0) {
			System.out.println(resultlist.get(0).getId());
			createnewData(resultlist.get(0).getId());
			return resultlist.get(0).getId() + 1;
		} else {
			return 0L;
		}
	}
}
